package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;


    //处理新增请求
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {//修改
            employeeService.update(employee, ids);//ALT+ENTER自动给接口加参数
        } else { //新增
            employeeService.save(employee, ids);//ALT+ENTER自动给接口加参数
        }
        return new JsonResult();

    }

    //处理去新增请求
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        //共享角色数据
        model.addAttribute("roles", roleService.listAll());
        List<Department> departments = departmentService.listAll();
        //查询所有部门往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("departments", departments);
        //若传 id 值,表示是去修改,那就要去查询被修改的部门对象,存在模型中
        if (id != null) { // 表示去修改
            Employee employee = employeeService.get(id);
            //System.out.println("查询的员工对象===" + employee);
            model.addAttribute("employee", employee);
        }
        // /WEB-INF/views/employee/input.jsp
        return "employee/input";
    }

    //处理删除部门请求
    @RequestMapping("/delete")
    private String delete(Long id) {
        //传了id值就删除对应的部门数据
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }


    // 处理分页查询部门请求
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {

        /*PageResult<Employee> pageResult = employeeService.query(qo);*/
        model.addAttribute("pageInfo", employeeService.query(qo));

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);

        return "employee/list"; // 找视图 /WEB-INF/views/employee/list.jsp
    }

    //去重置密码页面
    @RequestMapping("/resetPwd")
    public String resetPwd(Long id, Model model) {
        if (id != null) {
            model.addAttribute("user", employeeService.get(id));
        }
        return "pwd/resetPwd"; //WEB-INF/views/pwd/resetPwd.jsp
    }

    //重置按钮提交
    @RequestMapping("/resetPwdPage")
    @ResponseBody
    public JsonResult resetPwdPage(String newPassword, Long id) {
        if (id != null) {
            employeeService.resetPwdPage(newPassword, id);
        }
        return new JsonResult();

    }


    //去修改密码页面
    @RequestMapping("/updatePwd")
    public String updatePwd(Long id, Model model) {
        if (id != null) {
            model.addAttribute("user", employeeService.get(id));
        }
        return "pwd/updatePwd";
    }

    //修改按钮
    @RequestMapping("/updatePwdPage")
    @ResponseBody
    public JsonResult updatePwdPage(String oldPassword, String newPassword, Long id) {
        if (id != null) {
            employeeService.updatePwdPage(oldPassword, newPassword, id);
        }
        return new JsonResult();


    }

    //批量删除
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        employeeService.batchDelete(ids);
        return new JsonResult();
    }


    //远程验证
    //验证用户名是否存在
    //valid的key是插件规定的,不能改
    //true验证通过false验证不通过
    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name, Long id) {
        HashMap map = new HashMap<>();
        if (id == null) {
            Employee employee = employeeService.selectByName(name);
            map.put("valid", employee == null);

        }
        if (id != null) {
            Employee employee = employeeService.get(id);
            if (employee.getName().equals(name)) {
                map.put("valid", true);
            }
        }
        return map;

    }


    //导出
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件下载的响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //在业务层中创建excel
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出到浏览器的响应流中
        wb.write(response.getOutputStream());

    }

    //导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }


    //禁用/恢复账号功能
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id, int status) {
        if (id != null) {
            employeeService.updateStatus(id, status);
        }
        return new JsonResult();
    }
}